<template>
  <div class="app-container">
    <div class="filter-container filter-container-left">
      <el-form :inline="true" label-width='90px'>
        <el-form-item label="职位类型：" class="flex_box">
          <el-cascader
            v-model="queryParam.attr_value_id_str"
            :options="postTypeList"
            :props='optionProps'
            :show-all-levels="false"
            @change='filterPostChange'
            placeholder="请选择职位类型"
            class="filter-item form-search-input" 
            ></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button class="filter-item search" type='primary' icon="el-icon-search" @click="handleSearch">查询</el-button>
          <el-button class="filter-item" icon="el-icon-refresh" @click="handleRefresh">重置</el-button>
          <el-button class="filter-item" type="warning" icon="el-icon-plus" @click="handleCreate">添加保到岗规则</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="data"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
    >
      <!-- <el-table-column prop="id" label="序号" width='60' align='center'/> -->
      <el-table-column prop="attr_post_name" label="岗位类型" align='center'/>
      <el-table-column prop="type_name" label="行业类型" align='center'/>
      <el-table-column label="地区" align='center'>
        <template slot-scope="item">
          {{item.row.province_name}}{{item.row.city_name}}{{item.row.district_name}}
        </template>
      </el-table-column>
      <el-table-column label="时间段" align='center'>
        <template slot-scope="item">
          {{getDateType(item.row.date_type)}}
        </template>
      </el-table-column>
      <el-table-column prop="platform_money" label="平台酬劳" align='center'/>
      <el-table-column label="操作" align='center'>
        <template slot-scope="item">
          <el-link type="primary" class="table_btn" @click="handleUpdate(item.row)" :underline="false"><i class="iconfont icon-bianji"></i>编辑</el-link>
          <el-link type="danger" class="table_btn" @click="handleDelete(item.row.id)" :underline="false"><i class="iconfont icon-lajitong1"></i>删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      class="pagination-container"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="paginate.current"
      :hide-on-single-page='isHideSingle'
      :page-sizes="paginate.sizes"
      :page-size="paginate.limit"
      :layout="paginate.layout"
      :total="paginate.total"/>
    <el-dialog :visible.sync="formVisible" width='654px' :show-close='false'>
      <div slot="title" class="dialog-title">添加保到岗规则</div>
      <el-card class="el-card-border">
        <el-form :model="formFieldsData" :label-position="labelPosition" :ref='formName' label-width='120px' :rules='rules'>
          <el-form-item label="职位类型：" prop='attr_post_id'>
            <el-cascader
              v-model="formFieldsData.attr_post_id"
              :options="postTypeList"
              :props='optionProps'
              collapse-tags
              @change='filterPostChange1'
              placeholder="请选择职位类型"
              style="width:100%"
              ></el-cascader>
          </el-form-item>
          <el-form-item label="行业类别：" prop='profession_type'>
            <el-select v-model="formFieldsData.profession_type" placeholder="请选择行业类别" style="width:100%">
              <el-option :label="item.type_name" :value="item.id" v-for="item in profession" :key='item.id'></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="地区：" prop='city'>
            <addr @addrChange='addrChange' ref='addr'></addr>
          </el-form-item>
          <el-form-item label="时间段：" prop='date_type'>
          <el-radio-group v-model="formFieldsData.date_type">
            <el-radio :label="item.value" v-for="item in timeList" :key='item.value' :checked='item.value==formFieldsData.time'>{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
          <el-form-item label="平台酬劳(元)：" prop='platform_money'>
            <el-input v-model="formFieldsData.platform_money" autocomplete="off" placeholder='请输入平台酬劳'></el-input>
          </el-form-item>
        </el-form>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel" size='medium'>取 消</el-button>
        <el-button type="primary" @click="handleSubmit" size='medium'>确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import formOperate from '@/layout/mixin/formOperate'
import addr from '@/components/addr'
export default {
  name: 'Index',
  mixins: [formOperate],
  data() {
    return {
      url:'/promotion/admin/ProtectConfig',
      queryParam: {
        attr_post_id:'',
        page:1,
        page_size:10
      },
      defaultQueryParam:['page','page_size'],
      optionProps: {
        value: 'value_id',
        label: 'value_name',
        children: 'child'
      },
      formName:'addform',
      formFieldsData:{
        attr_post_id:'',
        profession_type:'',
        date_type:'',
        platform_money:'',
        province:'',
        city:'',
        district:''
      },
      profession:[],// 行业类别
      area:{},
      timeList:[
        {label:'白班',value:1},
        {label:'夜班',value:2},
        {label:'全天',value:3},
        {label:'长途',value:4}
      ],
      rules:{
        attr_post_id:[
          { required: true, message: '请选择职位类型', trigger: 'click' }
        ],
        profession_type:[
          { required: true, message: '请选择行业类别', trigger: 'click' }
        ],
        city:[
          { required: true, message: '请选择地区', trigger: 'click' }
        ],
        date_type:[
          { required: true, message: '请选择时间', trigger: 'click' }
        ],
        platform_money:[
          { required: true, message: '请输入平台酬劳', trigger: 'blur' }
        ]
      },
    }
  },
  mounted() {
    this.GetAttrData('POST_TYPE')
    this.getProfession()
  },
  methods: {
    getProfession() {
      this.$http.get('/promotion/admin/ProfessoinType',{params:{page_size:0}}).then((response) => {
        console.log(response)
        this.profession = response.data.data
      })
    },
    // 获取时间段
    getDateType(type) {
      let idx = this.timeList.findIndex(res=>{
        return res.value == type
      })
      return this.timeList[idx].label
    },
    // 提交成功后
    resetAddr() {
      this.$refs.addr.clearVal()
    },
    beforeUpdate(item) {
      this.area = {
        pid:item.province,
        pname:item.province_name,
        cid:item.city,
        cname:item.city_name,
        did:item.district,
        dname:item.district_name
      }
      this.$nextTick(()=>{
        this.$refs.addr.updateData(this.area)
      })
    },
    // 搜索职位类型改变
    filterPostChange(e) {
      this.queryParam.attr_post_id = e[1]
    },
    // 添加职位类型改变
    filterPostChange1(e) {
      this.formFieldsData.attr_post_id = e[1]
    },
    // 选择地区后
    addrChange(val) {
      this.formFieldsData.province = val.provinceid
      this.formFieldsData.city = val.cityid
      this.formFieldsData.district = val.districtid
    }
  },
  components:{
    addr
  }
}
</script>